package src.main.fagnwenzhe;
/**这个例子比较容易理解访问者模式。访问者模式是对多态的充分使用*/
public class Test005 {
    public static void main(String[] args) {
        Shapes s=new Yuan();//指定绘画形式：圆
        s.draw();
        s.AcceptVisitor(new ZoomIn());//访问者去寻找圆变大方法
        s.AcceptVisitor(new ZoonOut());//访问者去寻找圆变小方法
        System.out.println("============================");
        Shapes fang=new Fang();
        fang.draw();
        fang.AcceptVisitor(new ZoonOut());
        fang.AcceptVisitor(new ZoomIn());

    }
}

interface Shapes{
     void draw();
     void AcceptVisitor(IVisitor visitor); //关键方法，代替if判断，访问者用多态匹配具体类。
}
class Yuan implements Shapes{
    public void draw() {
        System.out.println("画圆形");
    }
    public void AcceptVisitor(IVisitor visitor) {//this很关键，代表此类对象。访问者通过动态匹配到此类，此方法跳转到访问者具体实现类进行操作。
        // TODO Auto-generated method stub
        visitor.zoomIn(this);
        visitor.zoomOut(this);
    }

}
class Fang implements Shapes{
    public void draw() {
        System.out.println("画方形");
    }
    public void AcceptVisitor(IVisitor visitor) {
        // TODO Auto-generated method stub
        visitor.zoomIn(this);
        visitor.zoomOut(this);
    }


}
class Xian implements Shapes{
    public void draw() {
        System.out.println("画线条");
    }
    public void AcceptVisitor(IVisitor visitor) {
        // TODO Auto-generated method stub
        visitor.zoomIn(this);
        visitor.zoomOut(this);
    }
}
/**接口默认方法可以不用全部实现*/
interface IVisitor{
    default void zoomIn(Xian xian) {}
    default void zoomIn(Yuan yuan) {}
    default void zoomIn(Fang fang) {}

    default void zoomOut(Xian xian) {}
    default void zoomOut(Yuan yuan) {}
    default void zoomOut(Fang fang) {}
}
/**变大*/
class ZoomIn implements IVisitor {
    @Override
    public void zoomIn(Yuan yuan) {
        // TODO Auto-generated method stub
        System.out.println("圆变大");
    }
    @Override
    public void zoomIn(Fang fang) {
        // TODO Auto-generated method stub
        System.out.println("方变大");
    }
    @Override
    public void zoomIn(Xian xian) {
        // TODO Auto-generated method stub
        System.out.println("线变大");
    }
}

/**变小*/
class ZoonOut  implements IVisitor{
    @Override
    public void zoomOut(Xian xian) {
        // TODO Auto-generated method stub
        System.out.println("线变小");
    }
    @Override
    public void zoomOut(Yuan yuan) {
        // TODO Auto-generated method stub
        System.out.println("圆变小");
    }
    @Override
    public void zoomOut(Fang fang) {
        // TODO Auto-generated method stub
        System.out.println("方变小");
    }
}